﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Entity;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading.Tasks;
using System.Configuration;
using EKAABSInformationManagement.Service;
using EKAABSInformationManagement.Models;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Controllers
{
    public class MyRequestController : Controller
    {
        public MyRequest MyRequest { get; private set; }
        public IAsyncSendingmail asyncSendmail { get; private set; }
        public string from { get; private set; }
        public string currentUser { get; private set; }
        public string currentUser_email { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (System.Web.HttpContext.Current.User.IsInRole("member") != true)
            {
                HttpException ex = new HttpException(401, "");
                requestContext.HttpContext.AddError(ex);
            }
            this.from = string.IsNullOrEmpty(ConfigurationManager.AppSettings["userName"]) ? "" : ConfigurationManager.AppSettings["userName"];
            this.from = this.from.Contains("@wicresoft.com") ? this.from : string.Concat(this.from, "@wicresoft.com");
            this.currentUser = Membership.GetUser().UserName;
            this.currentUser_email = Membership.GetUser().Email;
            if (MyRequest == null) { MyRequest = new MyRequest(); }
            if (asyncSendmail == null) { asyncSendmail = new asyncSendingmail(); }
            base.Initialize(requestContext);
        }
        //
        // GET: /Item/

        public ActionResult Index()
        {
            List<requirementRecords> items = MyRequest.getRecords(currentUser);
            return View(items);
        }

        [HttpPost]
        public ActionResult submit(requirementRecords a)
        {
            try
            {
                bool isExists = default(bool);
                MyRequest.insert(a, out isExists, currentUser);
                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    asyncSendmail.recipient = this.from;
                    asyncSendmail.ccRecipient = this.currentUser_email;
                    asyncSendmail.mailBody = string.Concat("Hi,\r\n\r\n来自“", currentUser, "”物品申请，详情如下：\r\n\r\n申请单号：", a.ID, "\r\n申请物品：", a.productName, "\r\n数量：", a.number, "\r\n描述：", a.description, "\r\n\r\nRegards\r\n  --");
                    asyncSendmail.mailSubject = "新提交的物品申请";
                    asyncSendmail.AsyncSendmail();
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult update(requirementRecords a)
        {
            try
            {
                MyRequest.update(a);
                asyncSendmail.recipient = this.from;
                asyncSendmail.ccRecipient = this.currentUser_email;
                asyncSendmail.mailBody = string.Concat("Hi,\r\n\r\n来自“", currentUser, "”物品申请已经更新，详情如下：\r\n\r\n申请单号：", a.ID, "\r\n申请物品：", a.productName, "\r\n数量：", a.number, "\r\n描述：", a.description, "\r\n\r\nRegards\r\n  --");
                asyncSendmail.mailSubject = "更新的物品申请";
                asyncSendmail.AsyncSendmail();
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult delete(string ids)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                List<requirementRecords> records = MyRequest.delete(strs);
                asyncSendmail.recipient = this.from;
                asyncSendmail.ccRecipient = this.currentUser_email;
                asyncSendmail.mailBody = string.Concat("Hi,\r\n\r\n来自“", currentUser, "”的下列物品申请已作废，详情如下：");
                foreach (requirementRecords r in records)
                {
                    asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\n申请单号：", r.ID, "\r\n申请物品：", r.productName, "\r\n数量：", r.number, "\r\n描述：", r.description);
                }
                asyncSendmail.mailBody = string.Concat(asyncSendmail.mailBody, "\r\n\r\nRegards\r\n  --");
                asyncSendmail.mailSubject = "作废的物品申请";
                asyncSendmail.AsyncSendmail();
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
    }
}
